package com.mycjj.android.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.StoreActivity;
import com.mycjj.android.bean.StoreActivityBaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.pullrefresh.PullToRefreshBase;
import com.mycjj.android.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.mycjj.android.pullrefresh.PullToRefreshListView;
import com.mycjj.android.util.Preferences;


public class PointBFragment extends BaseFragment{
	
	public static PointBFragment newInstance(String type) {
		PointBFragment fragment = new PointBFragment();
		Bundle bundle = new Bundle();
		bundle.putString("type", type);
		fragment.setArguments(bundle);
        return fragment;
    }
	private View rootView;
	private PullToRefreshListView mListView;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_main_new_car, null);
		mListView = (PullToRefreshListView)rootView.findViewById(R.id.lv_pull_refresh_list);
		mListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				//请求数据
				mListView.onRefreshComplete();//归原来
			}
		});
		return rootView;
	}
	private MainArticlesAdapter adapter;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
	}
	
	private void initData(){
		adapter = new MainArticlesAdapter(getActivity());
		
		ProjectHttpRequestInterface request = new ProjectHttpRequestImpl(getActivity(), true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String storeid = Preferences.getSharedPreferences(getActivity(), "storeId", "");
		params.put("store_id", storeid);
		params.put("token",
				Preferences.getSharedPreferences(getActivity().getApplicationContext(), "user_token", ""));
		request.requestStoreGetActivityWithDoneHandler("", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				StoreActivityBaseBean activityBean = ProjectDataManage
						.getInstance().getStoreActivityBaseBean();
				Log.d("PointActivityFragment", (null == activityBean)? "activityBean is null":"activityBean code"+ activityBean.getCode());
				if((null != activityBean) && (null != activityBean.getData()))
					{
						adapter.setList(activityBean.getData().getList());
					}
				}
			});
		
		
		mListView.setAdapter(adapter);
	}
	
	public class MainArticlesAdapter extends TAdapter<StoreActivity>{

		public MainArticlesAdapter(Context mContext) {
			super(mContext);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_main_tab, null);
				holder = new ViewHolder();
				holder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
				holder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
				holder.tv_desc = (TextView)convertView.findViewById(R.id.tv_desc);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			if(null != mList)
			{
				StoreActivity bean = mList.get(position);
				imageLoader.displayImage(bean.getActivity_banner(), holder.iv_pic);
				holder.tv_title.setText(bean.getActivity_title());
				holder.tv_desc.setText(bean.getActivity_desc());
			}
			return convertView;
		}
		
		@Override
		public int getCount() {
			if(null != mList)
			{
				return mList.size();
			}
			return 0;
		}
		
		class ViewHolder{
			ImageView iv_pic;
			TextView tv_title;
			TextView tv_desc;
		}
	}
}
